package com.foo.common.base.utils;

import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.slf4j.LoggerFactory;

import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;

/**
 * 同步一个目录下的所有文件到另外一个目录。请注意,目标目录会被清除以保证完全同步
 * 
 * @author Steve
 *
 */
public class DirectorySyncHelper {
	public static final String NEW_ARCHITECTURE_NBI_WEBSERVICE_SVN = "D:\\zzNode\\NEW_ARCHITECTURE_BRANCH\\DEV_BRANCH_SICHUAN\\SCNBIWEBSERVICE_DEV\\src\\com\\shtel";
	public static final String NEW_ARCHITECTURE_NBI_WEBSERVICE_GIT = "D:\\programTool\\myGit\\ims\\itmsPlus_Core\\src\\main\\java\\com\\shtel";
	final static ch.qos.logback.classic.Logger logger = (ch.qos.logback.classic.Logger) LoggerFactory
			.getLogger("shortStdout");

	public static void main(String[] args) throws IOException {

		File srcDir = new File(NEW_ARCHITECTURE_NBI_WEBSERVICE_SVN);
		File destDir = new File(NEW_ARCHITECTURE_NBI_WEBSERVICE_GIT);

		List<String> srcList = Lists.newArrayList();
		List<String> desList = Lists.newArrayList();

		for (File file : srcDir.listFiles()) {
			srcList.add(file.getName());
		}
		for (File file : destDir.listFiles()) {
			desList.add(file.getName());
		}
		Collections.sort(srcList);
		Collections.sort(desList);

		logger.info("srcList:{}", FooUtils.listToString(srcList));
		logger.info("desList:{}", FooUtils.listToString(desList));

		Preconditions.checkArgument(srcList.equals(desList),
				"srcDirectory and desDirectory do not equal");

		logger.info("clean destDir:【{}】now.", destDir.getPath());
		FileUtils.cleanDirectory(destDir);

		logger.info("beginning coping from【{}】to【{}】now.", srcDir.getPath(),
				destDir.getPath());
		FileUtils.copyDirectory(srcDir, destDir, true);

	}
}
